extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/log.h>
#include <libavcodec/avcodec.h>
}

#include <iostream>

void print_video_info(const char *filename) {
    AVFormatContext *formatContext = nullptr;

    // 打开视频文件
    if (avformat_open_input(&formatContext, filename, nullptr, nullptr) != 0) {
        std::cerr << "Error: Could not open the file." << std::endl;
        return;
    }

    // 获取文件流信息
    if (avformat_find_stream_info(formatContext, nullptr) < 0) {
        std::cerr << "Error: Could not find stream information." << std::endl;
        avformat_close_input(&formatContext);
        return;
    }

    // 打印媒体文件的基本信息
    av_dump_format(formatContext, 0, filename, 0);

    // 查找视频流
    int video_stream_index = -1;
    for (int i = 0; i < formatContext->nb_streams; i++) {
        if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_stream_index = i;
            break;
        }
    }

    if (video_stream_index == -1) {
        std::cerr << "Error: Could not find a video stream." << std::endl;
    } else {
        std::cout << "Found video stream at index: " << video_stream_index << std::endl;
    }

    // 清理
    avformat_close_input(&formatContext);
}

int main() {
    // 初始化FFmpeg库
    av_register_all();
    avformat_network_init();

    const char *filename = "test.mp4"; // 修改为你的视频文件路径
    print_video_info(filename);

    return 0;
}
